//Copyright (c) 2024 Deng Zicheng
//        "CarController" is licensed under Mulan PSL v2.
//        You can use this software according to the terms and conditions of the Mulan PSL v2.
//        You may obtain a copy of Mulan PSL v2 at:
//        http://license.coscl.org.cn/MulanPSL2
//        THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
//        See the Mulan PSL v2 for more details.
package com.sinan.myapplication;

import ohos.app.Context;
import ohos.bluetooth.ble.*;

import java.util.*;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class BlueTooth_BLE_Manager {

    // 获取中心设备管理对象
    private static final ScanCallback centralManagerCallback = new ScanCallback();
    private static BleCentralManager centralManager = null;

    private static   MyBlePeripheralCallback  connectionCallBack;

    private static boolean connect_State=false;



     public BlueTooth_BLE_Manager(Context context){
         centralManager=new BleCentralManager(context, centralManagerCallback);
    }

    /**
     * 开启扫描
     */
    public void scanBLE(){
        // 创建扫描过滤器然后开始扫描
        List<BleScanFilter> filters = new ArrayList<>();
        centralManager.startScan(filters);
    }

    public void scanStopBLE(){
        centralManager.stopScan();//停止扫描
        centralManagerCallback.clearInformation();//清除扫描记录
    }

    /**
     * 获取扫描结果
     * @return 扫描设备信息结果
     */
    public List<Map<String,String>> getAllDevice(){
        List<Map<String,String>> devices=new ArrayList<>();
        Map<String, ArrayList<String>> results = centralManagerCallback.getResults();
        results.forEach((key, value) -> {
            Map<String, String> map = new HashMap<>();
            map.put("name", key);
            map.put("address",value.get(0));
            map.put("RSSI",value.get(1));
            devices.add(map);
        });
        return devices;
    }

    /**
     * 发起连接
     * @param address 地址
     */
    public void connect(String address){
        BlePeripheralDevice peripheralDevice = BlePeripheralDevice.createInstance(address);
        connectionCallBack= new MyBlePeripheralCallback(peripheralDevice);
        peripheralDevice.connect(true, connectionCallBack);
    }


    /**
     * 向蓝牙设备发送数据
     * @param message 信息
     */
    public void bleWrite(String message){
        if (connectionCallBack!=null){
            connectionCallBack.bleWrite(message);
        }
    }

    /**
     * 获取连接状态
     * @return 连接状态
     */
    public static boolean isConnect_State() {
        return connect_State;
    }

    /**
     * 设置连接状态
     * @param connect_State 连接状态
     */
    public static void setConnect_State(boolean connect_State) {
        BlueTooth_BLE_Manager.connect_State = connect_State;
    }

    public void readRSSI() {
        try {
            connectionCallBack.readRSSI();
        }catch (Exception ignore){

        }
    }
}
